﻿using com.passit.pai.core.net.utility;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mysqlDemo
{
    public static class Tools
    {
        public static string getWebPost(RequestMethod requestMethod, string apiResource, Dictionary<string, string> parameters, object inputDto)
        {
            RestClient restClient = new RestClient("http://gateway.jlrdev.k2software.cn/");
            RestRequest restRequest = new RestRequest(apiResource, (Method)requestMethod);
            restRequest.RequestFormat = DataFormat.Json;
            if (parameters != null)
            {
                foreach (KeyValuePair<string, string> parameter in parameters)
                {
                    restRequest.AddParameter(parameter.Key, parameter.Value);
                }
            }
            if (inputDto != null)
            {
                restRequest.AddBody(inputDto);
            }
            restRequest.AddHeader("Authorization", "Bearer " + ConfigurationManager.AppSettings["RequestToken"]);
            IRestResponse restResponse = restClient.Execute(restRequest);
            if (!restResponse.IsSuccessful)
            {
                Logger.Error($"ApiInvoker.Request failed. StatusCode:{restResponse.StatusCode},  :{restResponse.StatusDescription}, ErrorMessage:{restResponse.ErrorMessage}, ErrorException:{restResponse.ErrorException}", MsgSource.Utility);
            }
            else
            {
                Logger.Info("ApiInvoker.Request end.", MsgSource.Utility);
            }
            return restResponse.Content;
        }

        public static bool IsPropertyExist(dynamic data, string propertyname)
        {
            if (data == null)
                return false;

            if (data is ExpandoObject)
                return ((IDictionary<string, object>)data).ContainsKey(propertyname);
            if (data is JObject)
                return ((JObject)data).Property(propertyname) != null;
            return data.GetType().GetProperty(propertyname) != null;
        }
    }

    public static class K2
    {

    }

    public class Project
    {

    }
}
